package com.itheima.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.common.result.PageResult;
import com.itheima.dto.YddAdvimgDTO;
import com.itheima.mapper.YddAdvimgMapper;
import com.itheima.pojo.YddAdvimg;
import com.itheima.service.IYddAdvimgService;
import com.itheima.vo.YddAdvimgVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class IYddAdvimgServiceImpl extends ServiceImpl<YddAdvimgMapper, YddAdvimg> implements IYddAdvimgService {
    @Autowired
    private YddAdvimgMapper yddAdvimgMapper;

    @Override
    public PageResult listAdvimg(YddAdvimgDTO dto) {
        //非空判断
        if(dto.getLimit()==null){
            dto.setLimit("5");
        }
        if(dto.getPage()==null){
            dto.setPage("1");
        }
        //开启分页
        PageHelper.startPage(Integer.parseInt(dto.getPage()),Integer.parseInt(dto.getLimit()));
        //获取广告图描述、广告图对应的广告位id
        String advimgdesc = dto.getAdvimgdesc();
        String advimgpos = dto.getAdvimgpos();
        //根据广告位描述、广告位名称获取符合条件的数据
        Page<YddAdvimgVO> yddPage = yddAdvimgMapper.listAdvpos(advimgdesc,advimgpos);
        List<YddAdvimgVO> yddList = yddPage.getResult();

        //封装返回结果
        PageResult pageResult = new PageResult();
        //获取总条数
        pageResult.setCount(yddPage.size());
        //获取当前页数
        pageResult.setCurrenPage(Integer.parseInt(dto.getPage()));
        //获取数据
        pageResult.setData(yddList);
        //获取当前页面显示数量
        pageResult.setPageSize(Integer.parseInt(dto.getLimit()));
        //获取总页数
        pageResult.setTotalPages(yddPage.getTotal());
        return pageResult;
    }
}
